#pragma once
#include <iostream>
#include <pthread.h>
#include <string>

namespace MutexModule
{
    class Mutex
    {
    public:
        Mutex(const Mutex&) = delete;
        Mutex& operator=(const Mutex&) = delete;

        Mutex()
        {
            pthread_mutex_init(&_lock, nullptr);
        }

        void Lock()
        {
            pthread_mutex_lock(&_lock);
        }

        void Unlock()
        {
            pthread_mutex_unlock(&_lock);
        }

        pthread_mutex_t* GetPointer()
        {
            return &_lock;
        }

        pthread_mutex_t GetSelf()
        {
            return _lock;
        }

        ~Mutex()
        {
            pthread_mutex_destroy(&_lock);
        }

    private:
        pthread_mutex_t _lock;
    };

    class LockGuard
    {
    public:
        LockGuard(const LockGuard&) = delete;
        LockGuard& operator=(const LockGuard&) = delete;

        LockGuard(Mutex& mutex)
            :_mutex(mutex)
        {
            _mutex.Lock();
        }

        ~LockGuard()
        {
            _mutex.Unlock();
        }
    private:
        Mutex &_mutex;
    };
}